/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.config.StatusEnum;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.PluginReview;
import me.zhengjie.modules.system.domain.vo.PluginReviewCreateVo;
import me.zhengjie.modules.system.domain.vo.PluginReviewQueryCriteria;
import me.zhengjie.modules.system.mapper.PluginReviewMapper;
import me.zhengjie.modules.system.service.PluginReviewService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author lxp
* @date 2024-11-11
**/
@Service
@RequiredArgsConstructor
public class PluginReviewServiceImpl extends ServiceImpl<PluginReviewMapper, PluginReview> implements PluginReviewService {

    private final PluginReviewMapper pluginReviewMapper;

    @Override
    public PageResult<PluginReview> queryAll(PluginReviewQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(pluginReviewMapper.findAll(criteria, page));
    }

    @Override
    public List<PluginReview> queryAll(PluginReviewQueryCriteria criteria){
        return pluginReviewMapper.findAll(criteria);
    }

    public List<PluginReview> queryByParentId(Long parentId, String status) {
        LambdaQueryWrapper<PluginReview> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PluginReview::getPluginId,parentId);
        if(StringUtils.isNotEmpty(status)) {
            wrapper.eq(PluginReview::getStatus,status);
        }
        return list(wrapper);
    }

    public PluginReview queryPluginDetailById(Long id) {
        PluginReview pluginReview = this.getById(id);
        if(pluginReview == null) {
            throw new BadRequestException(" 评论不存在 ");
        }
        return pluginReview;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PluginReviewCreateVo resources) {
        PluginReview review = new PluginReview();
        BeanUtils.copyProperties(resources,review);
        review.setStatus(StatusEnum.APPROVE.getCode());
        save(review);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PluginReview resources) {
        PluginReview pluginReview = getById(resources.getId());
        pluginReview.copy(resources);
        saveOrUpdate(pluginReview);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<PluginReview> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PluginReview pluginReview : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("插件ID", pluginReview.getPluginId());
            map.put("标题", pluginReview.getTitle());
            map.put("插件描述", pluginReview.getContent());
            map.put("状态：1启用、0禁用", pluginReview.getStatus());
            map.put("创建者", pluginReview.getCreateBy());
            map.put("更新者", pluginReview.getUpdateBy());
            map.put("创建日期", pluginReview.getCreateTime());
            map.put("更新时间", pluginReview.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}